﻿using System;
using System.Threading.Tasks;
using Top.Common;
using Top.PointStore.Business;
using Top.PointStore.Business.Models;
using Top.Web.Api;
namespace Top.PointStore
{
    public class ApiBase : WebApi
    {      
        /// <summary>
        /// 获取Context中传入的QueryString参数的Value
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <returns>参数的Value</returns>
        public static string QueryString(string paramName)
        {
            string result = string.Empty;
            if (WebApiContext.Current.RequestParams != null)
            {
                result = WebApiContext.Current.RequestParams.QueryString[paramName];
            }
            return result;
        }

        /// <summary>
        /// 获取Context中传入的QueryString参数的Value
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数的Value</returns>
        public static T Get<T>(string paramName, T defaultValue)
        {
            string result = QueryString(paramName);
            if (string.IsNullOrWhiteSpace(result))
            {
                result = Form(paramName);
            }
            if (string.IsNullOrWhiteSpace(result))
            {
                result = defaultValue.ToString();
            }
            var temp = Convert2.ToObject<T>(result, defaultValue);
            return temp;
        }

        /// <summary>
        /// 获取Context中传入的Form参数的Value
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <returns>参数的Value</returns>
        public static string Form(string paramName)
        {
            string result = string.Empty;
            if (WebApiContext.Current.RequestParams != null)
            {
                result = WebApiContext.Current.RequestParams.Form[paramName];
            }
            return result;
        }

        /// <summary>
        /// 获取Context中传入的Form参数的Value
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数的Value</returns>
        public static string Post(string paramName, string defalutValue)
        {
            string result = QueryString(paramName);
            if (string.IsNullOrWhiteSpace(result))
            {
                result = Form(paramName);
            }
            if (string.IsNullOrWhiteSpace(result))
            {
                result = defalutValue;
            }
            return result;
        }

        protected override async Task<Exception> OnInitUser()
        {
            await base.OnInitUser();
            var user = base.User;            
            if (user != null)
            {
                pUser = Top.Core.Convert3.To<Top.PointStore.User>(user);
                if (user.CrmUserID!=0)
                {                    
                    T_PointStoreAdminAuthority auth = await SystemManager.GetPointStoreAdminAuthority(user.CrmUserID);
                    pUser.PointStoreAuth = auth;
                }                
            }
            if (user==null)
            {
                Quit();
            }
            return null;
        }

        private User _pUser;

        public User pUser
        {
            get
            {
                return _pUser;
            }
            set {
                _pUser = value;
            }
        }


        public object Quit()
        {
            Session.RemoveAll();
            return Redirect("/admin/login");
        }

    }

}